package com.shuma.product.controller;

import cn.hutool.core.lang.UUID;
import com.shuma.common.vo.R;
import com.shuma.product.exception.BusinessException;
import com.shuma.product.exception.ErrorCode;
import com.shuma.product.service.DataService;
import com.shuma.product.service.ProductService;
import com.shuma.product.utils.QiniuUtils;
import com.shuma.product.utils.RegexUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Collections;


@CrossOrigin
@RestController
public class ProductController {
    @Autowired
    @Qualifier("dataServiceImpl")
    DataService dateService;

    @Autowired
    @Qualifier("productServiceImpl")
    ProductService productService;

    private static final Logger LOG = LoggerFactory.getLogger(ProductService.class);

    @GetMapping("exchangeVolume")
    private R exchangeVolume(@RequestParam("beginTime") String beginTime,
                             @RequestParam("endTime") String endTime) {
        if(beginTime == null || endTime == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if(beginTime.compareTo(endTime) > 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if(RegexUtils.isTimeInvalid(beginTime) || RegexUtils.isTimeInvalid(endTime)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        //前端时间格式为yyyy-MM-dd
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate begin = LocalDate.parse(beginTime, formatter);
        LocalDate end = LocalDate.parse(endTime, formatter);
        if(dateService == null) {
            System.out.println("dateService is null");
            return R.fail(500, "dateService is null");
        }
        return dateService.exchangeVolume(begin, end);
    }

    @GetMapping("sale")
    private R sale(@RequestParam("beginTime") String beginTime,
                   @RequestParam("endTime") String endTime) {
        if(beginTime == null || endTime == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if(beginTime.compareTo(endTime) > 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if(RegexUtils.isTimeInvalid(beginTime) || RegexUtils.isTimeInvalid(endTime)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate begin = LocalDate.parse(beginTime, formatter);
        LocalDate end = LocalDate.parse(endTime, formatter);

        if(dateService == null) {
            System.out.println("dateService is null");
            return R.fail(500, "dateService is null");
        }

        return dateService.sale(begin, end);

    }

    @GetMapping("exchangeMethod")
    private R exchangeMethod(@RequestParam("beginTime") String beginTime,
                             @RequestParam("endTime") String endTime) {

        if(beginTime == null || endTime == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if(beginTime.compareTo(endTime) > 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if(RegexUtils.isTimeInvalid(beginTime) || RegexUtils.isTimeInvalid(endTime)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }


        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate begin = LocalDate.parse(beginTime, formatter);
        LocalDate end = LocalDate.parse(endTime, formatter);

        if(dateService == null) {
            System.out.println("dateService is null");
            return R.fail(500, "dateService is null");
        }
        return dateService.exchangeMethod(begin, end);
    }

    @GetMapping("preview")
    private R preview(@RequestParam("id") String id) {
        LOG.info("ID：{}", id);
        Integer integerid;
        if (id == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        try{
            integerid = Integer.valueOf(id);
        } catch (NumberFormatException e) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if(productService == null) {
            System.out.println("productService is null");
            return R.fail(500, "productService is null");
        }

        return productService.preview(Collections.singletonList(integerid));
    }

    @GetMapping("allUser")
    private R allUser() {
        if(productService == null) {
            System.out.println("productService is null");
            return R.fail(500, "productService is null");
        }

        return productService.allUser();
    }

}
